home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / SDKs / ScreenLight™ 1.0.1 / CodeExamples / More Code / Jules.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-05  |  6.4 KB  |  239 lines  |  [TEXT/MMCC]

  1. #include <math.h>
  2. #include "ModuleRoutines.h"
  3. #include "Jules.h"
  4.  
  5.  
  6. /*--------------------------------------------
  7. SmoothWalk()
  8. ---------------------------------------------*/
  9.  
  10. void SmoothWalk(WindowPtr wind)
  11. {
  12.     JulesData    juleData;
  13.  
  14.     InitJules(&juleData, &wind->portRect);
  15.     
  16.     while(!Button())
  17.     {
  18.         GetNextPoint(&juleData);
  19.         JuliaStep(&juleData, 10, false); // Throw away first few points
  20.         JuliaStep(&juleData, 30, true); // Draw a bunch
  21.         if(Random() % 20 == 0)
  22.         {
  23.             JuliaStep(&juleData, kNumJuliaPts, true); // Once in a while, do 'em all
  24.         }
  25.     }
  26.     InvalRect(&wind->portRect);
  27. }
  28.  
  29. /*--------------------------------------------
  30. InitJules()    Sets up from the given window dimensions
  31. ---------------------------------------------*/
  32.  
  33. void InitJules(JulesDataPtr julePtr, Rect *monR)
  34. {
  35.     short        xdim, ydim;
  36.     double_t    pixelWidth, pixelHeight, xOffset, yOffset;
  37.     //RGBColor    draw = {64000, 0, 0};
  38.     RGBColor    draw;
  39.     
  40.     // set number of Jules pts
  41.     julePtr->numJuliaPts = kNumJuliaPts;
  42.  
  43.     // Set up scaling constant
  44. #if defined(powerc) || defined (__powerc)
  45.     julePtr->randDivider = 32767.0 * 1000.0;
  46. #else
  47.     julePtr->randDivider = 32767.0 * 100.0 * 3;    // added a few pts 'cause 881 is fast
  48. #endif
  49.     
  50.     // Get size of window
  51.     xdim = monR->right - monR->left;
  52.     ydim = monR->bottom - monR->top;
  53.  
  54.     // Calculate the # of pixels per unit distance for the julePtrs
  55.     julePtr->xPixPerUnit = (double_t)xdim / 3.0;
  56.     julePtr->yPixPerUnit = (double_t)ydim / 3.0;
  57.     
  58.     // •• Set up stuff for random walk ••
  59.     
  60.     // initial velocity
  61.     julePtr->deltaX = (double_t)Random() / julePtr->randDivider;
  62.     julePtr->deltaY = (double_t)Random() / julePtr->randDivider;
  63.  
  64.     // Find box in pixel space
  65.     julePtr->boundsLeft = (double_t)xdim / 6;
  66.     julePtr->boundsRight = (double_t)xdim - julePtr->boundsLeft;
  67.     julePtr->boundsTop = (double_t)ydim / 6;
  68.     julePtr->boundsBot = (double_t)ydim - julePtr->boundsTop;
  69.     
  70.     // Convert to Mandel Space
  71.     pixelWidth = 3.0 / (double_t)xdim;
  72.     pixelHeight = 2.4 / (double_t)xdim;
  73.     xOffset = 2.0;
  74.     yOffset = 1.2;
  75.     
  76.     julePtr->boundsLeft = -xOffset + (julePtr->boundsLeft * pixelWidth);
  77.     julePtr->boundsTop = -yOffset + (julePtr->boundsTop * pixelHeight);
  78.     julePtr->boundsRight = -xOffset + (julePtr->boundsRight * pixelWidth);
  79.     julePtr->boundsBot = -yOffset + (julePtr->boundsBot * pixelHeight);
  80.     
  81.     // Current point, start in center of bounds
  82.     julePtr->currentX = julePtr->boundsLeft + ((julePtr->boundsRight - julePtr->boundsLeft) / 2);
  83.     julePtr->currentY = julePtr->boundsTop + ((julePtr->boundsBot - julePtr->boundsTop) / 2);
  84.     
  85.     // •• Set up color ramp stuff ••
  86.     julePtr->drawColor = draw;
  87.     
  88.     // Set up color ramping increments
  89.     julePtr->curHue = 0;
  90.     //julePtr->rInc = Random() % 256;
  91.     //julePtr->gInc = Random() % 256;
  92.     //julePtr->bInc = Random() % 256;
  93.  
  94.     // Calculate half the window dimensions
  95.     julePtr->halfXDim = xdim / 2 + monR->left;
  96.     julePtr->halfYDim = ydim / 2 + monR->top;
  97.     
  98.     // set up initial julePtr points outside window
  99.     julePtr->ptIndex = 0;
  100.     {
  101.         short cnt;
  102.  
  103.         for(cnt = 0; cnt < kNumJuliaPts; cnt++)
  104.             julePtr->juliaPts[cnt].h = julePtr->juliaPts[cnt].v = -1;
  105.     }
  106. }
  107.  
  108.  
  109. void JuliaStep(JulesDataPtr julePtr, short numPts, Boolean drawIt)
  110. {
  111.     long            count;
  112.     short            m, n;
  113.     double_t        wx, wy, theta, r, x, y;
  114.     const double_t    pi = 3.14159, piOver2 = 1.570795;
  115.     
  116.     // Ramp the color
  117.     if ( julePtr->useColor && TickCount() %4 == 0) {
  118.         julePtr->curHue++; 
  119.         if ( julePtr->curHue > 360) 
  120.             julePtr->curHue = 0;
  121.         CalcHSBtoRGB( julePtr->curHue, 100, 100, &julePtr->drawColor);
  122.     }
  123.     //julePtr->drawColor.red += julePtr->rInc;
  124.     //julePtr->drawColor.green += julePtr->gInc;
  125.     //julePtr->drawColor.blue += julePtr->bInc;
  126.  
  127.     x = y = 0.1;
  128.     for(count = 0; count < numPts; count++)
  129.     {
  130.         wx = x - julePtr->currentX;
  131.         wy = y - julePtr->currentY;
  132.  
  133.         if(wx > 0.0)
  134.             theta = atan(wy/wx);
  135.         else if(wx < 0.0)
  136.             theta = pi + atan(wy/wx);
  137.         else
  138.             theta = piOver2;
  139.         
  140.         theta /= 2;
  141.         
  142.         r = sqrt(wx * wx + wy * wy);
  143.         if(Random() < 0)
  144.             r = sqrt(r);
  145.         else
  146.             r = -sqrt(r);
  147.         
  148.         x = r * cos(theta);
  149.         y = r * sin(theta);
  150.         
  151.         if(drawIt)
  152.         {
  153.             PenMode(srcCopy);
  154.             
  155.             // Erase old
  156.             MoveTo(julePtr->juliaPts[julePtr->ptIndex].h, julePtr->juliaPts[julePtr->ptIndex].v);
  157.             //RGBForeColor(&julePtr->eraseColor);
  158.             ForeColor(blackColor);
  159.             Line(0, 0);
  160.             
  161.             // Convert to window coords
  162.             julePtr->juliaPts[julePtr->ptIndex].h = m = (short)(x * julePtr->xPixPerUnit) + julePtr->halfXDim;
  163.             julePtr->juliaPts[julePtr->ptIndex].v = n = (short)(y * julePtr->yPixPerUnit) + julePtr->halfYDim;
  164.             julePtr->ptIndex++;
  165.             
  166.             // Draw new
  167.             MoveTo(m, n);
  168.             if ( julePtr->useColor) RGBForeColor(&julePtr->drawColor);
  169.              else ForeColor( whiteColor);
  170.             Line(0, 0);
  171.             
  172.             // Keep julePtr->ptIndex in range
  173.             if(julePtr->ptIndex >= kNumJuliaPts)
  174.                 julePtr->ptIndex %= kNumJuliaPts;
  175.         }
  176.     }
  177. }
  178.  
  179. void GetNextPoint(JulesDataPtr julePtr)
  180. {
  181.     double_t        localH, localV;
  182.     double_t        maxSpeed, minSpeed;
  183.  
  184.     localH = julePtr->currentX;
  185.     localV = julePtr->currentY;
  186.     
  187.     localH += julePtr->deltaX;
  188.     localV += julePtr->deltaY;
  189.     
  190. #if defined(powerc) || defined (__powerc)
  191.     maxSpeed = 0.004;
  192.     minSpeed = 0.0001;
  193. #else
  194.     maxSpeed = 0.04;
  195.     minSpeed = 0.001;
  196. #endif
  197.     
  198.     if(localH > julePtr->boundsRight)
  199.     {
  200.         localH = julePtr->boundsRight;
  201.         julePtr->deltaX = -julePtr->deltaX;
  202.         julePtr->deltaX += (double_t)Random() / julePtr->randDivider;
  203.         julePtr->deltaY += (double_t)Random() / julePtr->randDivider;
  204.     }
  205.     if(localH < julePtr->boundsLeft)
  206.     {
  207.         localH = julePtr->boundsLeft;
  208.         julePtr->deltaX = -julePtr->deltaX;
  209.         julePtr->deltaX += (double_t)Random() / julePtr->randDivider;
  210.         julePtr->deltaY += (double_t)Random() / julePtr->randDivider;
  211.     }
  212.     if(localV > julePtr->boundsBot)
  213.     {
  214.         localV = julePtr->boundsBot;
  215.         julePtr->deltaY = -julePtr->deltaY;
  216.         julePtr->deltaX += (double_t)Random() / julePtr->randDivider;
  217.         julePtr->deltaY += (double_t)Random() / julePtr->randDivider;
  218.     }
  219.     if(localV < julePtr->boundsTop)
  220.     {
  221.         localV = julePtr->boundsTop;
  222.         julePtr->deltaY = -julePtr->deltaY;
  223.         julePtr->deltaX += (double_t)Random() / julePtr->randDivider;
  224.         julePtr->deltaY += (double_t)Random() / julePtr->randDivider;
  225.     }
  226.     
  227.     // adjust speed
  228.     if(julePtr->deltaX > maxSpeed) julePtr->deltaX = maxSpeed;
  229.     else if(julePtr->deltaX < -maxSpeed) julePtr->deltaX = -maxSpeed;
  230.     else if(julePtr->deltaX == 0.0) julePtr->deltaX = 0.001;
  231.     if(julePtr->deltaY > maxSpeed) julePtr->deltaY = maxSpeed;
  232.     else if(julePtr->deltaY < -maxSpeed) julePtr->deltaY = -maxSpeed;
  233.     else if(julePtr->deltaY == 0.0) julePtr->deltaY = 0.001;
  234.     
  235.     // save new value in struct
  236.     julePtr->currentX = localH;
  237.     julePtr->currentY = localV;
  238. }
  239.